//
//  DrivesTabViewController.m
//  carcareIOS
//
//  Created by wr on 16/4/1.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "DrivesTabViewController.h"
#import "RadarPictureViewController.h"
#import "DrivesViewController.h"
#import "WaySummaryViewController.h"

#import "WRGestureViewController.h"
#import "MessageTestGPWViewController.h"
#import "SettingMgr.h"
#import "GestureLocMgr.h"
#import "PrivacySetViewController.h"

typedef NS_ENUM(NSInteger , Segmented_Type)
{
    Segmented_Type_Driver = 50,//行程
    Segmented_Type_Summar ,//统计
};

@interface DrivesTabViewController ()

@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@property (nonatomic, assign) NSInteger segmentType;//切换tab类型

@property (nonatomic , strong) DrivesViewController * drivesVc;

@property (nonatomic , strong) WaySummaryViewController *waySummaryVC;

@property (nonatomic , strong) UIView * driversView;

@property (nonatomic , strong) UIView * waySummaryView;


@property (nonatomic , strong) WRGestureViewController * checkWRVC;

//@property (nonatomic , strong) WRGestureViewController * messageCreateWRVC;

@end

@implementation DrivesTabViewController

#pragma  mark --- view live action
//- (void)dealloc
//{
//    if(self.segmentType == Segmented_Type_Driver) self.drivesVc.drivesMgr = nil;
//}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UmengMgr event:ICZ_Trip];
    if(self.segmentType == Segmented_Type_Driver)[self.drivesVc.hTableView reloadData];
    
    if(![Check isVisiter])
    {
        if (GESTURELOCMGR.getDrivePageIsGesture) {
            [self loadGestureVC];
        }
    }
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if(self.segmentType == Segmented_Type_Driver) [DRIVERSMGR loadPreloadDays];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"行程";
    
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"行程",@"统计"]];
    self.segmentedControl.frame = CGRectMake(0, 0, 234 * __MAIN_RATIO_375 , 23);
    [self.segmentedControl addTarget:self action:@selector(segmentedChange:) forControlEvents:UIControlEventValueChanged];
    self.segmentedControl.tintColor = [UIColor whiteColor];
    self.segmentedControl.selectedSegmentIndex = 0;
    self.segmentType = Segmented_Type_Driver;
    self.navigationItem.titleView = self.segmentedControl;
    
    [self createView];
    
}
#pragma mark - 加载手势密码页面
- (void)loadGestureVC
{
    if ([SETTINGMGR.gesturePWStr isNotNil] && SETTINGMGR.showGesturePassword) {
        __weak __typeof(self) wself = self;
        self.checkWRVC = [[WRGestureViewController alloc]initWithType:GestureV_Type_Check withPWOut:SETTINGMGR.showCanLookGesturePW withSuccess:^(NSString *passwordStr, BOOL success) {
            if (success) {
                [wself.checkWRVC.navigationController popViewControllerAnimated:NO];
                if(wself.checkWRVC.gestureViewType == GestureV_Type_Create)[AlertMgr showShortSuccessTextTips:@[@"手势密码设置成功"]];
            }else//手势密码验证次数过多导致失败
            {
                
                TipsView *tips = [TipsView warnTipsWithTitle:nil lightTitles:@[@"手势密码输入错误次数过多，您可以通过忘记手势密码来验证身份，或5分钟后再试"]];
                [tips addLeftButton:@"取消" clickLeft:^{
                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
                } rightButton:@"忘记手势密码" clickRight:^{
                    [AlertMgr dismiss:TYPE_VIEW_ALERT];
                    [wself.checkWRVC.navigationController pushViewController:[[[MessageTestGPWViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL withTestSuccess:^{
                        wself.checkWRVC.gestureViewType = GestureV_Type_Create;
                        wself.checkWRVC.isTraOutOfPW = SETTINGMGR.showCanLookGesturePW;
                        [wself.checkWRVC navSetupLeftTitle:@"" action:nil];
                    }] navSetupBackItemWithIdentification:BACK_ICON] animated:YES];
                    
                }];
                [AlertMgr showView:tips];
                
            }
        }];
        self.checkWRVC.lostPassword = ^()
        {
            
            [wself.checkWRVC.navigationController pushViewController:[[[MessageTestGPWViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_NORMAL withTestSuccess:^{
                wself.checkWRVC.gestureViewType = GestureV_Type_Create;
                wself.checkWRVC.isTraOutOfPW = SETTINGMGR.showCanLookGesturePW;
                [wself.checkWRVC navSetupLeftTitle:@"" action:nil];
            }] navSetupBackItemWithIdentification:BACK_ICON] animated:YES];
        };
        self.checkWRVC.gestureVCWillAppear = ^()
        {
            [GESTURELOCMGR getDrivePageIsGesture];
            wself.checkWRVC.isTraOutOfPW = SETTINGMGR.showCanLookGesturePW;
            wself.checkWRVC.isNoNeedGesturePW = !SETTINGMGR.showGesturePassword;
            [wself.checkWRVC navSetupLeftTitle:@"" action:nil];
            wself.checkWRVC.savedPassword = SETTINGMGR.gesturePWStr;
            
        };
        self.checkWRVC.savedPassword = SETTINGMGR.gesturePWStr;
        
        self.checkWRVC.hidesBottomBarWhenPushed = NO;
        
        [self.navigationController pushViewController:self.checkWRVC animated:YES];
    }else if([AISystemInfo sharedInstance].shouldShowDrivesSettingGesture)
    {
        __weak __typeof(self) wself = self;
        TipsView *tipsV = [TipsView textTipsWithTitle:nil lightTitles:@[@"车挣可以记录轨迹啦!",@"您可以到个人中心的设置页面",@"开启轨迹记录。"]];
        [tipsV addLeftButton:@"暂不开启" clickLeft:^{
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        } rightButton:@"去设置" clickRight:^{
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
            [wself clickOnSetSettingPage];
        }];
        [AlertMgr showView:tipsV];
        [AISystemInfo sharedInstance].shouldShowDrivesSettingGesture = NO;
    }

}
#pragma mark - 跳转到个人中心设置轨迹页面
- (void)clickOnSetSettingPage
{
    __weak __typeof(self) wself = self;
    PrivacySetViewController * vc = [[PrivacySetViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE];
    [vc navSetupLeftTitle:@"取消" action:^{
        [wself dismissViewControllerAnimated:YES completion:nil];
    }];
    [self presentViewController:[UINavigationController navigationControllerWithRootViewController:vc] animated:YES completion:nil];
}
- (void)reloadView
{
    [self.driversView setHidden:(self.segmentType == Segmented_Type_Driver ? NO : YES)];
    [self.waySummaryView setHidden:(self.segmentType == Segmented_Type_Summar ? NO : YES)];
}
- (void)createView
{
    self.drivesVc = [[DrivesViewController alloc] init];
    __weak __typeof(self) wself = self;
    self.drivesVc.horizontalCellClick = ^(NSInteger selectModelIndex)
    {
        RadarPictureViewController *vc = [[[RadarPictureViewController alloc] initWithSelectIndex:selectModelIndex] navSetupBackItemWithIdentification:BACK_ICON_TITLE];
        [wself.navigationController pushViewController:vc animated:YES];
        [UmengMgr event:ICZ_Trip_List];
    };
    self.driversView = self.drivesVc.view;
    [self.view addSubview:self.driversView];
    
    self.waySummaryVC = [[WaySummaryViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE];
    self.waySummaryView = self.waySummaryVC.view;
    [self.view addSubview:self.waySummaryView];
    
    [self reloadView];
}
- (void)segmentedChange:(UISegmentedControl *)segmented{
    switch (segmented.selectedSegmentIndex) {
        case 0:
            self.segmentType = Segmented_Type_Driver;
            break;
        case 1:
            self.segmentType = Segmented_Type_Summar;
            break;
            
        default:
            break;
    }
    [self reloadView];

}
- (void)showTodayData
{
    [self.drivesVc showTodayData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
